<?php

namespace app\home\controller;

use app\home\model\pyg_cart;
use app\home\model\pyg_category;
use think\facade\Db;

class Cart
{
    public function cart()
    {
        $obj = new pyg_category();
        $res = $obj->showCategory()->toArray();
        $showCategory = get_tree_list($res);

        $id = Session('id');
        $obe = new pyg_cart();
        $data = $obe->with(['showCart','showCarts'])->where('user_id',$id)->select()->toArray();
        $sumShow = 0;
        $countShow = 0;
        foreach ($data as $key=>$val)
        {
            $data[$key]['sum']=$val['number']*$val['price'];
        }
        foreach ($data as $val)
        {
            $sumShow = $sumShow+=$val['sum'];
        }
        foreach ($data as $val)
        {
            $countShow = $countShow+=$val['number'];
        }
        return \think\facade\View::fetch('cart/cart',['showCategory'=>$showCategory,'data'=>$data,'sumShow'=>$sumShow,'countShow'=>$countShow]);
    }
//    数量减一
    public function reduction($id)
    {
        $obj = new pyg_cart();
        $res = $obj->reduction($id);
        $show = Db::table('pyg_cart')->find($id);
        if($res)
        {
            return success("成功",$show);
        }
        return fail('失败');
    }
//    数量加一
    public function sumCount($id)
    {
        $obj = new pyg_cart();
        $res = $obj->sumCount($id);
        $show = Db::table('pyg_cart')->find($id);
        if($res)
        {
            return success("成功",$show);
        }
        return fail('失败');
    }

}